#!/bin/bash

DEFAULT_PORT=5101
DEFAULT_IP="127.0.0.1"
REMOTE_PORT=${1:-$DEFAULT_PORT}
REMOTE_IP=${2:-$DEFAULT_IP}

# 配置免密登录
if [ ! -f ~/.ssh/id_rsa.pub ];then
  echo '公钥不存在，正在生成SSH密钥对...'
  ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -N ""
else
  echo '公钥已存在'
fi

ssh-copy-id -i ~/.ssh/id_rsa.pub -p ${REMOTE_PORT} root@${REMOTE_IP}
# 拷贝install到远程主机
scp -r -P ${REMOTE_PORT} /workspace/build/install/* root@${REMOTE_IP}:/workspace

# 执行
ssh -i ~/.ssh/id_rsa -p ${REMOTE_PORT} root@${REMOTE_IP} <<EOF
export LD_LIBRARY=/workspace/lib:/usr/local/lib64:/usr/lib64:/lib64:$LD_LIBRARY
chmod -R +x /workspace/bin
/workspace/bin/main
EOF